import {hex2Buf} from "@/utils/appCommon/waveMath/hexbuf";

export function viRawWave2List(hex:string){
  const buf = hex2Buf(hex)
  const length = buf.byteLength
  let dataView = new DataView(buf);
  let arr = []
  for(let i=0; i<length; i += 4){
    const thisV = dataView.getFloat32(i,true)
    arr.push(thisV)
  }
  //console.log(arr)
  return arr
}

// 时间 = 点数/采样率 单位g
// 1g=9.8m/s2
// 比如采样率100K/s
// 点数1000
// 时间 = 1000 / 100K = 0.01s = 10ms
export function getViWaveX(waveList, vi_measure_freq) {
  let length = waveList.length
  let ret = []
  if(vi_measure_freq <1){
    vi_measure_freq = 1
  }
  const space = Number((1/vi_measure_freq))
  for(let i=0; i< length; i++){
    //保留5位小数
    let tmp = Math.round((space * i *1000 )*100000)/100000
    ret.push(tmp)
  }
  return {
    list: ret,
    unit : 'ms'
  }
}

export function getViFFTWaveX(fftWaveList, vi_measure_freq) {
  let length = fftWaveList.length
  let ret = []
  if(vi_measure_freq <1){
    vi_measure_freq = 1
  }
  const space = vi_measure_freq / (2*length)
  for(let i=0; i< length; i++){
    ret.push(space * i)
  }
  return {
    list: ret,
    unit : 'Hz'
  }
}
